$(function () {
    // var currentPage=getParameter("currentPage");
    //load(name1,currentPage);

    $("#searchBtn").click(function () {
        $("#findMusicBtn").removeClass("btn-default");
        $("#findMusicBtn").addClass("btn-success");
        $("#findSingerBtn").removeClass("btn-success");
        $("#findSingerBtn").addClass("btn-default");
        $("#findMusicListBtn").removeClass("btn-success");
        $("#findMusicListBtn").addClass("btn-default");
        $("#findUserBtn").removeClass("btn-success");
        $("#findUserBtn").addClass("btn-default");
        $("#findMusicWordBtn").removeClass("btn-success");
        $("#findMusicWordBtn").addClass("btn-default");
        var name = $("#searchMsg").val();
        if (name == null || name === '') alert("还没输入内容哦")
        console.log("用户输入：" + name);
        findMusic();
    });
});


//模糊查询歌曲
function findMusic() {
    $("#findMusicBtn").removeClass("btn-default");
    $("#findMusicBtn").addClass("btn-success");
    $("#findSingerBtn").removeClass("btn-success");
    $("#findSingerBtn").addClass("btn-default");
    $("#findMusicListBtn").removeClass("btn-success");
    $("#findMusicListBtn").addClass("btn-default");
    $("#findUserBtn").removeClass("btn-success");
    $("#findUserBtn").addClass("btn-default");
    $("#findMusicWordBtn").removeClass("btn-success");
    $("#findMusicWordBtn").addClass("btn-default");
    var name = $("#searchMsg").val();
    $.post("/music/music/findMusicsByName", {name: name}, function (data) {
        // console.log("findMusic的" + data);
        $res = $("#result");
        $res.empty();
        if (data.list != null) {
            //获取tbody
            $res.html("  <thead>\n" +
                "                    <tr>\n" +
                "                        <th></th>\n" +
                "                        <th>歌曲</th>\n" +
                "                        <th>歌手</th>\n" +
                "                        <th>操作</th>\n" +
                "                    </tr>\n" +
                "                    </thead>")
            //拼接
            var trlist = '';
            // var t = document.createElement("tbody");
            for (var i = 0; i < data.list.length; i++) {
                var tr = "<tr>\n" +
                    "                        <th scope=\"row\">" + ((data.currentPage - 1) * data.pageSize + i + 1) + "</th>\n" +
                    // "                        <td>" + data.list[i].musicname + "</td>\n" +
                    '<td>' + '<a href="musicInfo.html?mid='+data.list[i].mid+'">'+data.list[i].musicname+'</a>' + '</td>' +
                    "                        <td>" + data.list[i].singer.singername + "</td>\n" +
                    "                        <td>\n" +
                    "                            <div class=\"btn-group\">\n" +
                    "                                <a onclick='insertMusic(" + data.list[i].mid + ")' class=\"btn btn-xs btn-default\" href=\"#\" title=\"播放\" data-toggle=\"tooltip\"\n" +
                    "                                   data-original-title=\"播放\">\n" +
                    "                                    <i class=\"mdi mdi-arrow-right-drop-circle-outline\"></i>\n" +
                    "                                </a>\n" +
                    "                               <div class=\"btn-group\">\n" +
                    "                                   <a data-toggle=\"dropdown\" aria-haspopup=\"true\" aria-expanded=\"true\" class=\"btn btn-xs btn-default\" id=\"addBtn\" title=\"添加到歌单\" data-toggle=\"tooltip\"\n" +
                    "                                       data-original-title=\"添加到歌单\" onclick='addToList(" + data.list[i].mid + ")'>\n" +
                    "                                       <i class=\"mdi mdi-plus\"></i>\n" +
                    "                                    </a>\n" +
                    "                                   <ul class=\"dropdown-menu\" id='userMusicList' >\n" +
                    "                                   </ul>\n" +
                    "                               </div>" +
                    "                            </div>\n" +
                    "                        </td>\n" +
                    "                    </tr>"
                trlist += tr;
            }
            //2.3 将liList添加到ul中
            $res.append(trlist);
            //4.处理页面 前5后4
            //4.1 开始页数 = 当前页数-5
        }
    }, "json");
}

//模糊查询歌手
function findSinger() {
    $("#findSingerBtn").removeClass("btn-default");
    $("#findSingerBtn").addClass("btn-success");
    $("#findMusicBtn").removeClass("btn-success");
    $("#findMusicBtn").addClass("btn-default");
    $("#findMusicListBtn").removeClass("btn-success");
    $("#findMusicListBtn").addClass("btn-default");
    $("#findUserBtn").removeClass("btn-success");
    $("#findUserBtn").addClass("btn-default");
    $("#findMusicWordBtn").removeClass("btn-success");
    $("#findMusicWordBtn").addClass("btn-default");
    var name = $("#searchMsg").val();
    $.post("/music/singer/findSingersByName", {name: name}, function (data) {
        // console.log("findSinger的" + data.list[0].singername);
        $res = $("#result");
        $res.empty();
        if (data.list != null) {
            //获取tbody
            $res.html("  <thead>\n" +
                "                    <tr>\n" +
                "                        <th></th>\n" +
                "                        <th>歌手</th>\n" +
                "                    </tr>\n" +
                "                    </thead>")
            //拼接
            var trlist = '';
            for (var i = 0; i < data.list.length; i++) {
                var tr = "<tr>\n" +
                    "                        <th scope=\"row\">" + ((data.currentPage - 1) * data.pageSize + i + 1) + "</th>\n" +
                    "                        <td>" + data.list[i].singername + "</td>\n" +
                    "                    </tr>"
                trlist += tr;
            }
            $res.append(trlist);
        }
    }, "json");
}

//模糊查询歌单
function findMusicList() {
    $("#findSingerBtn").removeClass("btn-success");
    $("#findSingerBtn").addClass("btn-default");
    $("#findMusicBtn").removeClass("btn-success");
    $("#findMusicBtn").addClass("btn-default");
    $("#findMusicListBtn").removeClass("btn-default");
    $("#findMusicListBtn").addClass("btn-success");
    $("#findUserBtn").removeClass("btn-success");
    $("#findUserBtn").addClass("btn-default");
    $("#findMusicWordBtn").removeClass("btn-success");
    $("#findMusicWordBtn").addClass("btn-default");
    var name = $("#searchMsg").val();
    $.post("/music/musicList/findMusicListsByName", {name: name}, function (data) {
        // console.log("findMusicList的" + data);
        $res = $("#result");
        $res.empty();
        if (data.list != null) {
            //获取tbody
            $res.html(" <thead>\n" +
                "          <tr>\n" +
                "            <th></th>\n" +
                "            <th>歌单</th>\n" +
                "            <th>创建人</th>\n" +
                "            <th>操作</th>\n" +
                "           </tr>\n" +
                "        </thead>")
            //拼接
            var trlist = '';
            for (var i = 0; i < data.list.length; i++) {
                var tr = "<tr>\n" +
                    "                        <th scope=\"row\">" + ((data.currentPage - 1) * data.pageSize + i + 1) + "</th>\n" +
                    "                        <td><a href='../userhtml/musicListInfo.html?mlid="+data.list[i].mlid+"'> " + data.list[i].musiclistname + "</a></td>\n" +
                    "                        <td>" + data.list[i].user.username + "</td>\n" +
                    "                        <td>\n" +
                    "                            <div class=\"btn-group\">\n" +
                    "                                <a  onclick=\"changelist("+data.list[i].mlid+")\" class=\"btn btn-xs btn-default\" href=\"#\" title=\"播放\" data-toggle=\"tooltip\"\n" +
                    "                                   data-original-title=\"播放\">\n" +
                    "                                    <i class=\"mdi mdi-arrow-right-drop-circle-outline\"></i>\n" +
                    "                                </a>\n" +
                    // "                                <a class=\"btn btn-xs btn-default\" id=\"addBtn\" title=\"添加到歌单\" data-toggle=\"tooltip\"\n" +
                    // "                                   data-original-title=\"添加到歌单\">\n" +
                    // "                                    <i class=\"mdi mdi-plus\"></i>\n" +
                    // "                                </a>\n" +
                    "                            </div>\n" +
                    "                        </td>\n" +
                    "                    </tr>"
                trlist += tr;
            }
            $res.append(trlist);
        }
    }, "json")
}

//模糊查询用户
function findUser() {
    $("#findSingerBtn").removeClass("btn-success");
    $("#findSingerBtn").addClass("btn-default");
    $("#findMusicBtn").removeClass("btn-success");
    $("#findMusicBtn").addClass("btn-default");
    $("#findMusicListBtn").removeClass("btn-success");
    $("#findMusicListBtn").addClass("btn-default");
    $("#findUserBtn").removeClass("btn-default");
    $("#findUserBtn").addClass("btn-success");
    $("#findMusicWordBtn").removeClass("btn-success");
    $("#findMusicWordBtn").addClass("btn-default");
    var name = $("#searchMsg").val();
    $.post("/music/user/findUsersByName", {name: name}, function (data) {
        // console.log("findUser的" + data);
        $res = $("#result");
        $res.empty();
        var i=0
        if (data.list != null) {
            var trlist = '';
            for (var key in data.map) {
                var tr = " <tr>\n" +
                    "          <td><a href='userInfo.html?uid="+data.list[i].uid+"'>" + key + "</td>\n" +
                    "          <td>歌单：" + data.map[key] + "</td>\n" +
                    // "          <td>关注</td>\n" +
                    "     </tr>"
                trlist += tr;
                i++
            }
            $res.append(trlist);
        }
    }, "json")
}

//模糊查询歌词
function findMusicWord() {
    $("#findSingerBtn").removeClass("btn-success");
    $("#findSingerBtn").addClass("btn-default");
    $("#findMusicBtn").removeClass("btn-success");
    $("#findMusicBtn").addClass("btn-default");
    $("#findMusicListBtn").removeClass("btn-success");
    $("#findMusicListBtn").addClass("btn-default");
    $("#findUserBtn").removeClass("btn-success");
    $("#findUserBtn").addClass("btn-default");
    $("#findMusicWordBtn").removeClass("btn-default");
    $("#findMusicWordBtn").addClass("btn-success");
    var name = $("#searchMsg").val();
    $.post("/music/music/findMusicsByLyrics", {name: name}, function (data) {
        // console.log("findMusicWord的" + data);
        $res = $("#result");
        $res.empty();
        if (data.list != null) {
            //拼接
            var trlist = '';
            for (var i = 0; i < data.list.length; i++) {
                var tr = "<tr>\n" +
                    "                        <td>" +
                    "                            <div>" +
                    "                               <strong style='font-size:15px' >" + data.list[i].musicname + "——" + data.list[i].singer.singername + "</strong>" +
                    "                               <p>" + data.list[i].lyrics + "</p>" +
                    "                           </div>" +
                    "                        </td>\n" +
                    "                        <td>\n" +
                    "                            <div class=\"btn-group\">\n" +
                    "                                <a onclick='insertMusic(" + data.list[i].mid + ")' class=\"btn btn-xs btn-default\" href=\"#\" title=\"播放\" data-toggle=\"tooltip\"\n" +
                    "                                   data-original-title=\"播放\">\n" +
                    "                                    <i class=\"mdi mdi-arrow-right-drop-circle-outline\"></i>\n" +
                    "                                </a>\n" +
                    "                                 <div class=\"btn-group\">\n" +
                    "                                   <a data-toggle=\"dropdown\" aria-haspopup=\"true\" aria-expanded=\"true\" class=\"btn btn-xs btn-default\" id=\"addBtn\" title=\"添加到歌单\" data-toggle=\"tooltip\"\n" +
                    "                                       data-original-title=\"添加到歌单\" onclick='addToList(" + data.list[i].mid + ")'>\n" +
                    "                                       <i class=\"mdi mdi-plus\"></i>\n" +
                    "                                    </a>\n" +
                    "                                   <ul class=\"dropdown-menu\" id='userMusicList'>\n" +
                    "                                   </ul>\n" +
                    "                               </div>" +
                    "                            </div>\n" +
                    "                        </td>\n" +
                    "                    </tr>"
                trlist += tr;
            }
            $res.append(trlist);
        }
    }, "json")
}

function addToList(mid) {
    console.log("添加到歌单")
    $.post("/music/user/isLogin", {}, function (data) {
        console.log("登录的用户" + data)
        if (data) {
            $.post("/music/user/getMusicList", {}, function (data) {
                //获取该用户的创建歌单
                console.log("用户创建的歌单：" + data[0].musiclistname);
                $list = $(".dropdown-menu");
                $list.empty();
                var list = ""
                for (var i = 0; i < data.length; i++) {
                    var li = "<li><a onclick='add(" + mid + "," + data[i].mlid + ")'>" + data[i].musiclistname + "</a></li>"
                    list += li;
                }
                list += " <li role='separator' class='divider'></li>" +
                    "    <li><a onclick='addToNewList(" + mid + ")' data-toggle='modal' data-target='#exampleModal' data-whatever='歌单名'>添加到新歌单</a></li>"
                $list.append(list);
            })
        } else {
            $list = $(".dropdown-menu");
            $list.empty();
            var list = " <li role='separator' class='divider'></li>" +
                "    <li><a href='userlogin.html'>登录后添加到歌单</a></li>"
            $list.html(list);
        }
    })
}

function add(mid, mlid) {
    $.post("/music/user/add", {mid: mid, mlid: mlid}, function () {
        alert("添加成功！");
    })
}

function sub() {
    var musiclistname = $("#musiclistname").val();
    var mid = $("#mid").val();
    console.log("mid:" + mid + ",musiclistname:" + musiclistname)
    if (musiclistname != null) {
        $.post("/music/musicList/userAddList", {musiclistname: musiclistname, mid: mid}, function (data) {
            if (data) {
                $("#errorMsg").css("display", "none");
                alert("添加成功!")
            } else {
                $("#errorMsg").css("display", "block");
                $("#errorMsg").html("歌单名已存在，建议修改！");
            }
        })
    }else {
        $("#errorMsg").css("display", "block");
        $("#errorMsg").html("歌单名不能为空");
    }
}

